package cn.liwq.demo.java.raw.concurrent;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: demo-common </p>
 * <p>Function:  </p>
 * <p>Description:  </p>
 * <p>Copyright: Copyright(c) 2018 Ucarinc </p>
 * <p>Company: Ucarinc </p>
 *
 * @author weiqiang.li@ucarinc.com
 * @version 1.0
 * @date 2018-08-15 10:47:11
 */
public class ThreadDemo {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("t1 run at " + i);
                }
                notify();
            }
        });
        Thread waitThread = new Thread(new WaitWorker(t1));
        waitThread.start();
        t1.start();
    }
}

class WaitWorker implements Runnable {

    private Thread other;

    WaitWorker(Thread o) {
        other = o;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        synchronized (other) {
            System.out.println(name + " is waitting other concurrent");
            try {
                other.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + " do sth after wait");
        }
    }
}